package djboom.materials.display {
	import djboom.materials.Work;
	import djboom.materials.core.BitmapDataCore;
	import djboom.materials.display.page.Page;
	import djboom.materials.events.MainPaneEvent;
	import djboom.materials.events.PageEvent;

	import org.casalib.display.CasaBitmap;
	import org.casalib.display.CasaSimpleButton;
	import org.casalib.display.CasaSprite;

	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.ui.ContextMenu;
	import flash.utils.getDefinitionByName;

	/**
	 * Контент.
	 * <br>
	 * 
	 * @author DJ BooM 
	 * @since 10.02.2011
	 * 
	 */

	public class MainPane extends CasaSprite {

		// ----------------------------------------------------------------------------
		// Константы класса.
		// ----------------------------------------------------------------------------

		private static const GLOW_FILTER:GlowFilter = new GlowFilter(0x1d1d1d, 1, 10, 10, 1);

		// ----------------------------------------------------------------------------
		// Свойства класса.
		// ----------------------------------------------------------------------------

		private var _page:Page;
		private var _commonInterfaceBitmap:CasaBitmap;
		private var _exitButton:CasaSimpleButton;

		private var _currentPage:String = Page.INDEX;

		// ----------------------------------------------------------------------------
		// Конструктор.
		// ----------------------------------------------------------------------------

		public function MainPane() {
			super();
			
			var backgroundItem:Sprite = new Sprite();
			backgroundItem.graphics.beginFill(0xFFFFFF);
			backgroundItem.graphics.drawRect(0, 0, Work.WIDTH, Work.HEIGHT);
			backgroundItem.filters = [GLOW_FILTER]; 
			addChild(backgroundItem);
			
			var developerCMenu:ContextMenu = new ContextMenu();
			developerCMenu.hideBuiltInItems();
            
			this.contextMenu = developerCMenu;
			
			createPage(_currentPage);
		}

		// ----------------------------------------------------------------------------
		// Приватные методы.
		// ----------------------------------------------------------------------------
		
		/**
		 * 
		 * 
		 */

		private function createPage(currentPageName:String):void {
			if (currentPageName != Page.INDEX) {
				if (_commonInterfaceBitmap == null) { 
					_commonInterfaceBitmap = new CasaBitmap(BitmapDataCore.getBitmapData(BitmapDataCore.COMMON_INTERFACE));
				}
                
				if (_exitButton == null) {
					var closeButtonBitmap:CasaBitmap = new CasaBitmap(BitmapDataCore.getBitmapData(BitmapDataCore.BUTTON_EXIT));
					_exitButton = new CasaSimpleButton(closeButtonBitmap, closeButtonBitmap, closeButtonBitmap, closeButtonBitmap);
					_exitButton.x = 985;
					_exitButton.y = 14;
					_exitButton.tabEnabled = false;
					_exitButton.addEventListener(MouseEvent.CLICK, exitWork);                   
				}
                
				addChild(_commonInterfaceBitmap);
			}
            
			var PageClass:Class = getDefinitionByName(currentPageName) as Class;
			_page = new PageClass();
			_page.addEventListener(PageEvent.CHANGE, setCurrentPage);
			_page.init();
			addChild(_page);
            
			if (_exitButton != null) addChild(_exitButton);
		}

		/**
		 * 
		 * 
		 */

		private function changePage():void {
			_page.destroy();
			_page = null;
            
			createPage(_currentPage);
		}

		/**
		 * 
		 * 
		 */

		private function setCurrentPage(event:PageEvent):void {
			_currentPage = event.pageName;
            
			changePage();
		}

		/**
		 * 
		 * 
		 */	

		private function exitWork(event:MouseEvent):void {
			dispatchEvent(new MainPaneEvent(MainPaneEvent.EXIT));
		}
	}
}
